<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="supplierGoodId">
        <el-input
          v-model="queryParams.supplierGoodId"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户名ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户名ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="开始时间" prop="orderStartTime">
        <el-date-picker
          v-model="queryParams.orderStartTime"
          type="date"
          placeholder="请选择开始时间"
          clearable
          :editable="false"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="orderEndTime">
        <el-date-picker
          v-model="queryParams.orderEndTime"
          type="date"
          placeholder="请选择结束时间"
          clearable
          :editable="false"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:item:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:item:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:item:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="订单id"
        align="center"
        prop="orderId"
        show-overflow-tooltip
      />
      <el-table-column
        label="用户名称"
        align="center"
        prop="userName"
        show-overflow-tooltip
      />

      <el-table-column
        label="用户ID"
        align="center"
        prop="userId"
        show-overflow-tooltip
      />
      <el-table-column
        label="商品名称"
        align="center"
        prop="goodName"
        show-overflow-tooltip
      />
      <el-table-column label="商品价格" align="center" prop="payMoney" />
      <el-table-column label="商品规格" align="center" prop="goodSpecs" />
      <el-table-column label="收件人" align="center" prop="receiveName" />
      <el-table-column
        label="收件人电话"
        align="center"
        prop="receivePhonenum"
      />
      <el-table-column
        label="收件人地址"
        align="center"
        prop="receiveAddress"
        show-overflow-tooltip
      />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="{ row }">
          {{ forrmatterEnmu(orderStatus, row.orderStatus) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:item:edit']"
            >详情</el-button
          >
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:item:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="商品分类id" prop="goodTypeId">
          <el-input v-model="form.goodTypeId" placeholder="请输入商品分类id" />
        </el-form-item>
        <el-form-item label="商品分类名称" prop="goodTypeName">
          <el-input
            v-model="form.goodTypeName"
            placeholder="请输入商品分类名称"
          />
        </el-form-item>
        <el-form-item label="供应商商品id" prop="supplierGoodId">
          <el-input
            v-model="form.supplierGoodId"
            placeholder="请输入供应商商品id"
          />
        </el-form-item>
        <el-form-item label="商品id" prop="storeGoodId">
          <el-input v-model="form.storeGoodId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="form.goodName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品价格" prop="goodPrice">
          <el-input v-model="form.goodPrice" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="商品数量" prop="goodCount">
          <el-input v-model="form.goodCount" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listItem,
  getItem,
  delItem,
  addItem,
  updateItem,
} from "@/api/system/order/manage";
import { forrmatterEnmu } from "@/utils/index";

export default {
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单项目表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderStartTime: null,
        orderEndTime: null,
        orderId: null,
        supplierGoodId: null,
        nickName: null,
        userId: null,
        phonenumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      orderStatus: [
        {
          name: "待付款",
          value: 0,
        },
        {
          name: "待发货",
          value: 1,
        },
        {
          name: "配送中",
          value: 2,
        },
        {
          name: "待收货",
          value: 3,
        },
        {
          name: "已完成",
          value: 4,
        },
        {
          name: "已退单",
          value: 5,
        },
        {
          name: "已关闭",
          value: 6,
        },
        {
          name: "退款中",
          value: 7,
        },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    forrmatterEnmu,
    /** 查询订单项目列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        goodTypeId: null,
        goodTypeName: null,
        supplierGoodId: null,
        storeGoodId: null,
        goodName: null,
        goodPrice: null,
        goodCount: null,
        createTime: null,
        updateTime: null,
        remarks: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getItem(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单项目编号为"' + ids + '"的数据项？')
        .then(function () {
          return delItem(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/item/export",
        {
          ...this.queryParams,
        },
        `item_${new Date().getTime()}.xlsx`
      );
    },
    /**查看详情 */
    handleDetail(row) {
      this.$router.push(`/order/manage/detail?id=${row.orderId}`);
      // window.open(`/order/manage/detail?id=${row.orderId}`);

      // this.$router.push()
    },
  },
};
</script>
